﻿using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Messaging.Dao;

namespace CkSoftware.GroupMe.Sdk.Messaging.Commands
{
	/// <summary>
	/// Direct messages create command (GroupMe documentation: <see href="https://dev.groupme.com/docs/v3#direct_messages_create" />)
	/// </summary>
	public class DirectMessagesCreateCommand : IWebApiCommand<GroupMeApiResponse<GroupMeMessageData>>
	{
		private readonly DirectMessagesCreateParameters _parameters;

		/// <summary>
		/// Instantiate a new <see cref="DirectMessagesCreateCommand" /> using the given parameters.
		/// </summary>
		/// <param name="memberId">The memberId of the recipient (attention, must be the UserId, not the Id of a group user).</param>
		/// <param name="sourceGuid">A identifier for the message, used for client-side message deduplication.</param>
		public DirectMessagesCreateCommand(string memberId, string sourceGuid)
		{
			_parameters = new DirectMessagesCreateParameters
			{
				Data = new DirectMessageCreateData
				{
					SourceGuid = sourceGuid,
					RecipientId = memberId
				}
			};
		}

		/// <summary>
		/// Defines if the command is executed once.
		/// </summary>
		public bool CommandExecuted { get; set; }

		/// <summary>
		/// The name of the command to be executed.
		/// </summary>
		public string CommandName
		{
			get { return "direct_messages"; }
		}

		/// <summary>
		/// The http method to execute this command.
		/// </summary>
		public HttpMethod HttpMethod
		{
			get { return HttpMethod.Post; }
		}

		/// <summary>
		/// The parameters which should be used on command execution.
		/// </summary>
		public IWebApiCommandParameters Parameters
		{
			get { return _parameters; }
		}

		/// <summary>
		/// Validates the current direct-messages parameters.
		/// </summary>
		/// <returns>True if there is at least a text or an attachment available, otherwise false.</returns>
		public bool IsParametersValid()
		{
			return !string.IsNullOrEmpty(_parameters.Data.Text) || _parameters.Data.Attachments.Any();
		}

		/// <summary>
		/// Adds an attachment to the current <see cref="DirectMessagesCreateCommand" />.
		/// </summary>
		/// <param name="attachment">The attachment to add to the message.</param>
		/// <returns>The extended <see cref="DirectMessagesCreateCommand" />.</returns>
		public DirectMessagesCreateCommand WithAttachment(GroupMeAttachmentBase attachment)
		{
			var attachments = new List<GroupMeAttachmentBase>(_parameters.Data.Attachments)
			{
				attachment
			};

			_parameters.Data.Attachments = attachments;

			return this;
		}

		/// <summary>
		/// Adds a text to the current message. Will replace the text if there is already a text defined.
		/// </summary>
		/// <param name="text">The text to add to the message.</param>
		/// <returns>The extended <see cref="DirectMessagesCreateCommand" />.</returns>
		public DirectMessagesCreateCommand WithText(string text)
		{
			_parameters.Data.Text = text;
			return this;
		}
	}
}